package com.ithuangyl.common.loader;

import android.content.Context;

import com.ithuangyl.common.dialog.LoaderDialog;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

/**
 * author : itHuang
 * e-mail : ithuangyl@163.com
 * date   : 2020/6/22 11:30
 */

public class LatteLoader {

    private static final ArrayList<DialogFragment> LOADERS = new ArrayList<>();

    private static final String DEFAULT_LOADER = LoaderStyle.BallClipRotatePulseIndicator.name();

    public static void showLoading(Context context, Enum<LoaderStyle> type) {
        showLoading(context, type.name());
    }

    private static void showLoading(Context context, String type) {
        final LoaderDialog dialog = LoaderDialog.create(type);
        LOADERS.add(dialog);
        dialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "LoaderDialog" + LOADERS.size());
    }

    public static void showLoading(Context context) {
        showLoading(context, DEFAULT_LOADER);
    }

    public static void stopLoading() {
        for (DialogFragment dialog : LOADERS) {
            if (dialog != null) {
                if (dialog.getDialog() != null && dialog.getDialog().isShowing()) {
                    dialog.dismiss();
                }
            }
        }
        LOADERS.clear();
    }
}
